<?php
setcookie("yuwen", $_POST['yuwen'],time()+2592000);
setcookie("shuxue", $_POST['shuxue'],time()+2592000);
setcookie("yingyu", $_POST['yingyu'],time()+2592000);
setcookie("kexue", $_POST['kexue'],time()+2592000);
setcookie("shehui", $_POST['shehui'],time()+2592000);
setcookie("zhengzhi", $_POST['zhengzhi'],time()+2592000);
setcookie("youxiu", $_POST['youxiu'],time()+2592000);
setcookie("jige", $_POST['jige'],time()+2592000);
setcookie("fn", $_POST['fn'],time()+2592000);
setcookie("ln", $_POST['ln'],time()+2592000);
setcookie("fxpercent", $_POST['fxpercent'],time()+2592000);
setcookie("lxpercent", $_POST['lxpercent'],time()+2592000);
?>
<html>
    <head>
        <STYLE>
            body, table, tr, td
                {
                font-size:   14px;
                font-family: 宋体
                }

            .border
                {
                border: thin solid #666666;
                }

            table
                {
                border:          2px solid #666666;
                border-collapse: collapse;
                margin-bottom:   15px;
                }

            td
                {
                padding: 1pt 6pt;
                border: 1px solid #666666;
                border-collapse: collapse;
                }

            .title
                {
                font-size:     20px;
                margin-bottom: 2px;
                }

            .titleb
                {
                font-size:     16px;
                margin-bottom: 8px;
                }
        </STYLE>
        <style media = print>
            .Noprint
                {
                display: none;
                }

            .PageNext
                {
                page-break-after: always;
                }
        </style>
        <meta http-equiv = "Content-Type" content = "text/html; charset=utf-8">
    </head>

    <body bgcolor = "#ffffff" text = "#000000"  topmargin = "0" leftmargin = "10" marginwidth = "0" marginheight = "0" link = "#000000"    vlink = "#000000" alink = "#000000">
        <div class = "Noprint"><a href = "#tj">统计</a><a href = "#pm">排名</a>
        <p>
<?php
error_reporting (0); // E_ALL & ~E_NOTICE
require_once 'Excel/reader.php';
//require "excelparser.php";
function getmicrotime() {
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
}
function show_time() {
    global $time_start,$time_end;
    $time = $time_end - $time_start;
    echo "分析完成时间： $time 秒<hr size=1>";
}
function fatal($msg = '') {
    echo '[致命错误]';
    if( strlen($msg) > 0 )
        echo ": $msg";
    echo "<br>\n脚本被终止<br>\n";
    if( $f_opened) @fclose($fh);
    exit();
};
$err_corr = "不支持的文件格式或文件已损坏";

$excel_file = $_FILES['excel_file'];
if( $excel_file )
    $excel_file = $_FILES['excel_file']['tmp_name'];
if( $excel_file == '' ) fatal("文件未上传");

$data = new Spreadsheet_Excel_Reader(); 
$data->setOutputEncoding('utf-8');

    $time_start = getmicrotime();
    $data->read($excel_file);
    $time_end = getmicrotime();
show_time();
    $time_start = getmicrotime();    
?>
        </p>
        </div>
<?php
$student = array(); //excle表全部数据
$subject = array();//科目列表（除0：学号 1：姓名 2：班级）
$ii = -1;       
//"第一行" 
for($j = 0; $j <= $data->sheets[0]['numCols']-1; $j++) {
    $subject[$j]=$data->sheets[0]['cells'][1][$j+1];
}         
//数据行          
for($i = 1; $i <= $data->sheets[0]['numRows']-1; $i++) {
    $ii++;    
    for($j=0;$j<=$data->sheets[0]['numCols']-1;$j++) {
        $student[$ii][$subject[$j]]=$data->sheets[0]['cells'][$i+1][$j+1];    
        }
}

$subject[]='总分';
$title=$_POST['title1']." ".$_POST['title2'];
$titleb=$_POST['titleb1']." ".$_POST['titleb2']." ".$_POST['titleb3'].$_POST['titleb4'].$_POST['titleb5'];
$curMarkTotal=array($_POST['yuwen'],$_POST['shuxue'],$_POST['yingyu'],$_POST['kexue'],$_POST['shehui'],$_POST['zhengzhi']);//各科总分
$goodMark = $_POST['youxiu'];//优秀系数
$passMark = $_POST['jige'];//及格系数
$classMark = array();//班级成绩
$fb=array();//成绩分布
$totalMark=array();
//$curMarkTotal数组在用到的分数后加上总分
$curMarkTotal[count($subject)-4]=0;
for($i=0;$i<count($subject)-4;$i++){
    $curMarkTotal[count($subject)-4]+=$curMarkTotal[$i];
}

for ($i=0;$i<=$ii;$i++){
    //$student[$i][$subject[2]:班级编号
    //if(!isset($classMark[strval($student[$i][$subject[2]])]["stuTotal"])) $classMark[strval($student[$i][$subject[2]])]["stuTotal"]=0;
    $classMark[strval($student[$i][$subject[2]])]["stuTotal"] += 1;//班级人数累加
    for ($t=3;$t<count($subject)-1;$t++){
        //计算每个人的总分
        $student[$i]["总分"]+=$student[$i][$subject[$t]];
    }
    for ($j=3;$j<count($subject);$j++){
        //某班"$subject[$j]"学科的"优秀、及格"人数
        $classMark[$student[$i][$subject[2]]]["good".$subject[$j]] += ($student[$i][$subject[$j]]>=($curMarkTotal[$j-3] * $goodMark));
        $classMark[$student[$i][$subject[2]]]["pass".$subject[$j]] += ($student[$i][$subject[$j]]>=($curMarkTotal[$j-3] * $passMark));
        //全年级"$subject[$j]"学科的"优秀、及格"人数
        $totalMark[$j-3]["good"] += ($student[$i][$subject[$j]]>=($curMarkTotal[$j-3] * $goodMark));
        $totalMark[$j-3]["pass"] += ($student[$i][$subject[$j]]>=($curMarkTotal[$j-3] * $passMark));
        //某班"$subject[$j]"学科成绩累加
        $classMark[$student[$i][$subject[2]]]["avg".$subject[$j]] += $student[$i][$subject[$j]];
        //全年级"$subject[$j]"学科成绩累加
        $totalMark[$j-3]["avg"] += $student[$i][$subject[$j]];
    }
}
 
 //各班各科优秀率、及格率、总分（无意义）、平均分
reset ($classMark);
for ($i=0;$i<count($classMark);$i++){
    for ($j=3;$j<count($subject);$j++){
        $classMark[key($classMark)]["good".$subject[$j]] /= ($classMark[key($classMark)]["stuTotal"]/100);
        $classMark[key($classMark)]["pass".$subject[$j]] /= ($classMark[key($classMark)]["stuTotal"]/100);
        //$classMark[key($classMark)]["all".$subject[$j]] = $classMark[key($classMark)]["avg".$subject[$j]];
        $classMark[key($classMark)]["avg".$subject[$j]] /= $classMark[key($classMark)]["stuTotal"];    
    }
    next ($classMark);
}

//年级各科优秀率、及格率、平均分
for($i=0;$i<count($totalMark);$i++){
    $totalMark[$i]["good"] /= (count($student)/100);
    $totalMark[$i]["pass"] /= (count($student)/100);
    $totalMark[$i]["avg"] /= count($student);
}

//计算标准差
for ($i=0;$i<=$ii;$i++){
    //for ($j=3;$j<count($subject)-1;$j++){
    for ($j=3;$j<count($subject);$j++){
        //$student[$i]["总分"]+=$student[$i][$subject[$j]];
        $classMark[$student[$i][$subject[2]]]["S".$subject[$j]] += pow (($student[$i][$subject[$j]]-$classMark[$student[$i][$subject[2]]]["avg".$subject[$j]]),2);
    }
}
reset ($classMark);
for ($i=0;$i<count($classMark);$i++){
    //for ($j=3;$j<count($subject)-1;$j++){
    for ($j=3;$j<count($subject);$j++){
        $classMark[key($classMark)]["S".$subject[$j]] = pow(($classMark[key($classMark)]["S".$subject[$j]]/$classMark[key($classMark)]["stuTotal"]),0.5);
        $classMark[key($classMark)]["S_t".$subject[$j]] = -$classMark[key($classMark)]["S".$subject[$j]];
    }
    next ($classMark);
}

//计算年级标准差

for ($i=0;$i<=$ii;$i++){
    //for ($j=3;$j<count($subject)-1;$j++){
    for ($j=3;$j<count($subject);$j++){
        $totalMark[$j-3]["S"] += pow (($student[$i][$subject[$j]]-$totalMark[$j-3]["avg"]),2);
    }
}
//for ($i=0;$i<count($classMark);$i++){
for ($i=0;$i<count($totalMark);$i++){
    $totalMark[$i]["S"] = pow(($totalMark[$i]["S"]/count($student)),0.5);

}


reset($classMark);

$classMark_H = array_keys($classMark);//$classMark数组的纵列标题
$classMark_V = array_keys($classMark[key($classMark)]);//$classMark数组的横列标题



//各班各科名次排名
for ($i=1;$i<count($classMark_V);$i++){
    for ($j=0;$j<count($classMark_H);$j++){
        $classMark[$classMark_H[$j]][$classMark_V[$i].'_pm']=0;
    }
}
for ($i=1;$i<count($classMark_V);$i++){
    $pm=1;
    $nowMAX=-100;
    $pmFlag=false;
    for ($j=0;$j<count($classMark_H);$j++){
        $tmpMAX=-100;
        for($k=0;$k<count($classMark_H);$k++){
            if ($classMark[$classMark_H[$k]][$classMark_V[$i].'_pm']==0){
                if (abs($classMark[$classMark_H[$k]][$classMark_V[$i]]-$tmpMAX)<0.000001){
                }elseif ($classMark[$classMark_H[$k]][$classMark_V[$i]]>$tmpMAX){
                    $tmpMAX=$classMark[$classMark_H[$k]][$classMark_V[$i]];
                    $tmpMark=$k;
                }
            }
        }
        if (abs($classMark[$classMark_H[$tmpMark]][$classMark_V[$i]]-$nowMAX)<0.000001){
            $classMark[$classMark_H[$tmpMark]][$classMark_V[$i].'_pm']=($pm);
        }else {
            $classMark[$classMark_H[$tmpMark]][$classMark_V[$i].'_pm']=($j+1);
            $nowMAX=$classMark[$classMark_H[$tmpMark]][$classMark_V[$i]];
            $pm=$j+1;
        }
    }
}

//成绩分布统计
function fb($i,$j){
    global $student,$subject,$curMarkTotal;
    $t =  $student[$i][$subject[$j]] / $curMarkTotal[$j-3] *100 %100;
    if ($t>=90){
        return 0;
    }elseif ($t>=85){
        return 1;
    }elseif ($t>=30){
        return 10-$t/10 %10;
    }else{
        return 8;
    }
}
for ($i=0;$i<count($student);$i++){
    for ($j=3;$j<count($subject);$j++){
        $fb[$j-3][$student[$i][$subject[2]]][fb($i,$j)]+=1;
        $fb[$j-3]["total"][fb($i,$j)]+=1;      
    }
}
?>
<?php
//总分排序
function px(&$arr,$orderby,$tii,$seq=true){//排序函数 $arr:被排序数组;$orderby:以哪里项排列;$tii:总人数$ii;$seq:true递增false递减
    for ($i=0;$i<=$tii-1;$i++){
        for ($j=0;$j<=$tii-$i;$j++){
            if($seq){
                if ($arr[$j][$orderby]<$arr[$j+1][$orderby]&&$arr[$j+1]){
                    $tmparr=$arr[$j];$arr[$j]=$arr[$j+1];$arr[$j+1]=$tmparr;
                }
            }else{
                if ($arr[$j][$orderby]>$arr[$j+1][$orderby]&&$arr[$j+1]){
                    $tmparr=$arr[$j];$arr[$j]=$arr[$j+1];$arr[$j+1]=$tmparr;
                }
            }
        }
    }
}

px($student,"总分",$ii);

/*for ($i=0;$i<=$ii-1;$i++){
    for ($j=0;$j<=$ii-$i;$j++){
        if ($student[$j]["总分"]<$student[$j+1]["总分"]){
            $tmparr=$student[$j];$student[$j]=$student[$j+1];$student[$j+1]=$tmparr;
        }
    }
}*/

//标记名次
$tmpmc=1;
for ($i=0;$i<=$ii;$i++){
    if ($student[$i]["总分"]==$student[$i-1]["总分"]){
        $student[$i]["mc"] = $tmpmc;
    }else{
        $student[$i]["mc"] = $tmpmc = $i+1;
    }
}
?>
<a name="tj"></a>
<?php
//------------------------------------
/*print "<td></td>";
for ($j=3;$j<count($subject)-1;$j++){
    print "<td>".$subject[$j]." 优秀率</td>";
    print "<td>".$subject[$j]." 及格率</td>";
    print "<td>".$subject[$j]." 平均分</td>";
    print "<td>".$subject[$j]." 标准差</td>";
}*/
print("<div class=\"title\">".$title."</div><div class=\"titleb\">".$titleb."</div><table><tbody><tr><td>学科</td><td>班级</td><td>人数</td><td>优秀率</td><td>排名</td><td>及格率</td><td>排名</td><td>平均分</td><td>排名</td><td>标准差</td><td>排名</td></tr>");
for ($j=3;$j<count($subject);$j++){
    reset ($classMark);
    for ($i=0;$i<count($classMark);$i++){
        print "<tr>";
        if ($i==0) {
            print "<td rowspan=\"".(count($classMark)+1)."\"><center>".$subject[$j]."</td>";}
            print "<td><center>".key($classMark)."</center></td>";
            print "<td align=\"right\">".$classMark[key($classMark)]["stuTotal"]."</td>";
            print "<td align=\"right\">".round($classMark[key($classMark)]["good".$subject[$j]],2)."%</td>";
            print "<td><center>".$classMark[key($classMark)]["good".$subject[$j]."_pm"]."</center></td>";
            print "<td align=\"right\">".round($classMark[key($classMark)]["pass".$subject[$j]],2)."%</td>";
            print "<td><center>".$classMark[key($classMark)]["pass".$subject[$j]."_pm"]."</center></td>";
            print "<td align=\"right\">".round($classMark[key($classMark)]["avg".$subject[$j]],2)."</td>";
            print "<td><center>".$classMark[key($classMark)]["avg".$subject[$j]."_pm"]."</center></td>";
            print "<td align=\"right\">".round($classMark[key($classMark)]["S".$subject[$j]],3)."</td>";
            print "<td><center>".$classMark[key($classMark)]["S_t".$subject[$j]."_pm"]."</center></td>";
            //print "<td align=\"right\">".round($classMark[key($classMark)]["all".$subject[$j]],2)."</td>";
            //print "<td><center>".$classMark[key($classMark)]["all".$subject[$j]."_pm"]."</center></td>";
        print "</tr>";
        next ($classMark);
    }
    print "<td align=\"center\">总计</td><td></td>";
    print "<td align=\"right\">".round($totalMark[$j-3]["good"],2)."%</td><td></td><td align=\"right\">".round($totalMark[$j-3]["pass"],2)."%</td><td></td><td align=\"right\">".round($totalMark[$j-3]["avg"],2)."</td><td></td><td align=\"right\">".round($totalMark[$j-3]["S"],3)."</td><td></td>";    
}
print "</tbody></table>";
//print_r($curMarkTotal);
//print_r($subject);
?>
<?php
print "<div class=\"PageNext\"></div>";
print "<div class=\"title\">".$title."</div><div class=\"titleb\">".$titleb." 分布表</div><table>";
for ($i=0;$i<(count($subject)-1-3+1);$i++){
    print "<tr><td rowspan=\"".(count($classMark_H)+2)."\">".$subject[$i+3]."</td><td>班级</td><td>".$curMarkTotal[$i]."-".$curMarkTotal[$i]*0.9."</td><td>".($curMarkTotal[$i]*0.9-1)."-".round($curMarkTotal[$i]*0.85,0)."</td><td>".(round($curMarkTotal[$i]*0.85,0)-1)."-".$curMarkTotal[$i]*0.8."</td><td>".($curMarkTotal[$i]*0.8-1)."-".$curMarkTotal[$i]*0.7."</td><td>".($curMarkTotal[$i]*0.7-1)."-".$curMarkTotal[$i]*0.6."</td><td>".($curMarkTotal[$i]*0.6-1)."-".$curMarkTotal[$i]*0.5."</td><td>".($curMarkTotal[$i]*0.5-1)."-".$curMarkTotal[$i]*0.4."</td><td>".($curMarkTotal[$i]*0.4-1)."-".$curMarkTotal[$i]*0.3."</td><td>".($curMarkTotal[$i]*0.3)."以下</td>";
    for($j=0;$j<count($classMark_H);$j++){
        print "<tr><td>".$classMark_H[$j]."</td>";
        for ($k=0;$k<9;$k++){
            print "<td>".$fb[$i][$classMark_H[$j]][$k]."</td>";
        }
        print "</tr>";
    }
    print "<tr><td>总计</td>";
    for ($k=0;$k<9;$k++){
        print "<td>".$fb[$i]["total"][$k]."</td>";
    }
    print "</tr>";
    
}
print "</table>";
?>
<br/><a name="pm"></a>
<?php
/*print "排名表<table><tbody>";
for ($i=0;$i<=$ii;$i++){
    print "<tr><td>$i</td>";
    for ($j=0;$j<count($subject)-1;$j++){
        print "<td>".$student[$i][$subject[$j]]."</td>";
    }
    print "<td>".$student[$i]["总分"]."</td><td>".$student[$i]["mc"]."</td>";
    print "</tr>\n";
}
print "</tbody></table>\n<br>\n";*/

//各班排名
for ($i=0;$i<count($classMark_H);$i++){
    print "<div class=\"PageNext\"></div>"."<div class=\"title\">".$title."</div><div class=\"titleb\">".$titleb." (".$classMark_H[$i].")</div><table><tbody>";
    $bjpm=1;
    $bjpm_t=1;
    $bjpm_tt=0;
    print "<tr><td>学号</td>";
    print "<td>姓名</td>";
        for ($k=3;$k<count($subject)-1;$k++){
            print "<td>".$subject[$k]."</td>";
        } 
    print "<td>总分</td><td>班级名次</td><td>年级名次</td></tr>";
    for ($j=0;$j<count($student);$j++){
        if ($student[$j][$subject[2]]==$classMark_H[$i]){
            print "<tr><td>".$student[$j][$subject[0]]."</td>";
            print "<td>".$student[$j][$subject[1]]."</td>";
            for ($k=3;$k<count($subject)-1;$k++){
                print "<td>".$student[$j][$subject[$k]]."</td>";
            } 
            print "<td>".$student[$j]["总分"]."</td>";
            if($bjpm_tt==$student[$j]["总分"]){
                print "<td>".$bjpm_t."</td>";
                $student[$j]["bjmc"]=$bjpm_t;
            }else{
                $bjpm_tt=$student[$j]["总分"];
                print  "<td>".$bjpm."</td>";
                $student[$j]["bjmc"]=$bjpm;
                $bjpm_t=$bjpm;
            }
            print "<td>".$student[$j]["mc"]."</td></tr>";
            $bjpm++;
        }
    }
    print "</tbody></table>";
}
?>
<?php
//-----------前n名--------
$fn=$_POST['fn'];
print "<div class=\"PageNext\"></div>"."<div class=\"title\">".$title."</div><div class=\"titleb\">".$titleb." (前".$fn."名)</div><table><tbody><tr>";
for ($i=0;$i<count($subject)-1;$i++){
    print "<td>".$subject[$i]."</td>";
}
print "<td>总分</td><td>班级名次</td><td>年级名次</td></tr>";
for ($i=0;$i<$fn;$i++){
    if ($student[$i]){ //判断是否有学生
        print "<tr>";
        for ($j=0;$j<count($subject)-1;$j++){
            print "<td>".$student[$i][$subject[$j]]."</td>";
        }
        print "</td>";
        print "<td>".$student[$i]["总分"]."</td><td>".$student[$i]["bjmc"]."</td><td>".$student[$i]["mc"]."</td></tr>";
    }
}
print "</tbody></table>";

//----------后n名
$ln=$_POST['ln'];
print "<div class=\"PageNext\"></div>"."<div class=\"title\">".$title."</div><div class=\"titleb\">".$titleb." (后".$ln."名)</div><table><tbody><tr>";
for ($i=0;$i<count($subject)-1;$i++){
    print "<td>".$subject[$i]."</td>";
}
print "<td>总分</td><td>班级名次</td><td>年级名次</td></tr>";
for ($i=count($student)-1;$i>count($student)-1-$ln;$i--){
    if ($student[$i]){ //判断是否有学生
        print "<tr>";
        for ($j=0;$j<count($subject)-1;$j++){
            print "<td>".$student[$i][$subject[$j]]."</td>";
        }
        print "</td>";
        print "<td>".$student[$i]["总分"]."</td><td>".$student[$i]["bjmc"]."</td><td>".$student[$i]["mc"]."</td></tr>";
    }
}
print "</tbody></table>";
//----------以下是各班各科前后n%所占比例情况---------
$fp=array();//前n%
$lp=array();//后n%
$fxpercent = $_POST['fxpercent'];
$lxpercent = $_POST['lxpercent'];
$fxpercentm =($ii+1)*$fxpercent/100;
$lxpercentm =($ii+1)*$lxpercent/100;

for($h=3;$h<count($subject);$h++){
    px($student,$subject[$h],$ii);
    $i=0;
    while($i<=$fxpercentm-1 or ($i>$fxpercentm-1 && $student[$i][$subject[$h]]==$student[$i-1][$subject[$h]])){
        $fp[$h-3]["totle"]+=1;
        $fp[$h-3][array_search($student[$i][$subject[2]],$classMark_H)]+=1;
        $i++;
    }

}

//输出表格
print "<div class=\"PageNext\"></div>"."<div class=\"title\">".$title."</div><div class=\"titleb\">".$titleb."<br>全年级各科 前".$fxpercent."% 分布情况</div><table><tbody><tr><td></td>";
for ($i=3;$i<count($subject);$i++){
    print "<td colspan=\"2\">".$subject[$i]."</td>";
}
print "</tr><tr><td>班级</td>";
for ($i=3;$i<count($subject);$i++){
    print "<td>人数</td><td>百分比</td>";
}
print "</tr>";
for ($i=0;$i<count($classMark_H);$i++){
    print "<tr><td>".$classMark_H[$i]."</td>";
    for ($j=0;$j<count($subject)-3;$j++){
        print "<td align=\"right\">".$fp[$j][$i]."</td><td align=\"right\">".(floor($fp[$j][$i]/$fp[$j]["totle"]*100+0.5))."%</td>";
    }
    print "</tr>";
}
print "</tbody></table>";
//---------------------
for($h=3;$h<count($subject);$h++){
    px($student,$subject[$h],$ii,false);
    $i=0;
    while($i<=$lxpercentm-1 or ($i>$lxpercentm-1 && $student[$i][$subject[$h]]==$student[$i-1][$subject[$h]])){
        $lp[$h-3]["totle"]+=1;
        $lp[$h-3][array_search($student[$i][$subject[2]],$classMark_H)]+=1;
        $i++;
    }

}

print "<div class=\"titleb\"><br>全年级各科 后".$lxpercent."% 分布情况</div><table><tbody><tr><td></td>";
for ($i=3;$i<count($subject);$i++){
    print "<td colspan=\"2\">".$subject[$i]."</td>";
}
print "</tr><tr><td>班级</td>";
for ($i=3;$i<count($subject);$i++){
    print "<td>人数</td><td>百分比</td>";
}
print "</tr>";
for ($i=0;$i<count($classMark_H);$i++){
    print "<tr><td>".$classMark_H[$i]."</td>";
    for ($j=0;$j<count($subject)-3;$j++){
        print "<td align=\"right\">".$lp[$j][$i]."</td><td align=\"right\">".(floor($lp[$j][$i]/$lp[$j]["totle"]*100+0.5))."%</td>";
    }
    print "</tr>";
}
print "</tbody></table>";

$time_end = getmicrotime();
?>
<div class = "Noprint">
<?php
show_time();
?>
</div>
</body>
</html>
